package com.java110.mall.chain.cmd.partner;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.partnerAdapt.PartnerAdaptDto;
import com.java110.dto.partnerAdapt.PartnerGoodsDto;
import com.java110.dto.partnerAdapt.PartnerPoolDto;
import com.java110.intf.chain.IPartnerAdaptV1InnerServiceSMO;
import com.java110.intf.chain.IPartnerPoolV1InnerServiceSMO;
import com.java110.intf.goods.IPartnerGoodsV1InnerServiceSMO;
import com.java110.mall.chain.partner.IPartnerAdapter;
import com.java110.utils.exception.CmdException;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "partner.syncPartnerGoodsStock")
public class SyncPartnerGoodsStockCmd extends Cmd {

    @Autowired
    private IPartnerPoolV1InnerServiceSMO partnerPoolV1InnerServiceSMOImpl;

    @Autowired
    private IPartnerGoodsV1InnerServiceSMO partnerGoodsV1InnerServiceSMOImpl;

    @Autowired
    private IPartnerAdaptV1InnerServiceSMO partnerAdaptV1InnerServiceSMOImpl;
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含商铺");
        Assert.hasKeyAndValue(reqJson, "ppId", "未包含供货商");

        PartnerGoodsDto partnerGoodsDto = new PartnerGoodsDto();
        partnerGoodsDto.setPpId(reqJson.getString("ppId"));
        partnerGoodsDto.setShopId(reqJson.getString("shopId"));
        List<PartnerGoodsDto> partnerGoodsDtos = partnerGoodsV1InnerServiceSMOImpl.queryPartnerGoodss(partnerGoodsDto);

        if (ListUtil.isNull(partnerGoodsDtos)) {
            throw new CmdException("商品不存在");
        }


    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        PartnerPoolDto partnerPoolDto = new PartnerPoolDto();
        partnerPoolDto.setPpId(reqJson.getString("ppId"));
        partnerPoolDto.setShopId(reqJson.getString("shopId"));
        List<PartnerPoolDto> partnerPoolDtos = partnerPoolV1InnerServiceSMOImpl.queryPartnerPools(partnerPoolDto);
        if (ListUtil.isNull(partnerPoolDtos)) {
            throw new CmdException("合作商不存在");
        }

        PartnerAdaptDto partnerAdaptDto = new PartnerAdaptDto();
        partnerAdaptDto.setPartnerType(partnerPoolDtos.get(0).getPartnerType());
        List<PartnerAdaptDto> partnerAdaptDtos = partnerAdaptV1InnerServiceSMOImpl.queryPartnerAdapts(partnerAdaptDto);

        if (ListUtil.isNull(partnerAdaptDtos)) {
            throw new CmdException("合作商不支持");
        }

        IPartnerAdapter partnerAdapter = ApplicationContextFactory.getBean(partnerAdaptDtos.get(0).getBeanImpl(), IPartnerAdapter.class);

        if (partnerAdapter == null) {
            throw new CmdException("合作商未实现");
        }

        ResultVo resultVo = partnerAdapter.syncGoodsStock(partnerPoolDtos.get(0));

        context.setResponseEntity(ResultVo.createResponseEntity(resultVo));

    }
}
